const InventoryModel = require('../model/InventoryModel');

class InventoryController {
     constructor() {
        this.inventoryModel = new InventoryModel();
    }
    /** 获取库存列表 */
    async getInventory(req, res) {
        try {
            const params = req.query;
            const result = await this.inventoryModel.getInventory(params);
            return res.json({
                code: 0,
                msg: "success",
                data: result,
            });
        } catch (error) {
            console.error(error.message);
            return res.status(500).json({
                code: 1,
                msg: "服务器内部错误，获取库存列表失败。",
            });
        }
    }
    /** 获取商品信息 */
    async getgoodsinfo(req, res) {
        try {
            const { goods_id } = req.params;
            const result = await this.inventoryModel.getgoodsinfo(goods_id);
            return res.json({
                code: 0,
                msg: "success",
                data: result,
            });
        } catch (error) {
            console.error(error.message);
            return res.status(500).json({
                code: 1,
                msg: "服务器内部错误，获取商品信息失败。",
            });
        }
    }
    /** 获取仓库信息 */
    async getHouseInfo(req, res) {
        try {
            const  house_id  = req.query.house_id;
            const result = await this.inventoryModel.getHouseInfo(house_id);
            return res.json({
                code: 0,
                msg: "success",
                data: result,
            });
        } catch (error) {
            console.error(error.message);
            return res.status(500).json({
                code: 1,
                msg: "服务器内部错误，获取仓库信息失败。",
            });
        }
    }
    /** 修改库存信息 */
    async putInventoryInfo(req, res) {
        try {
            const  goods_id  = req.body.goods_id;
            const params = req.body;
            const result = await this.inventoryModel.putInventoryInfo(goods_id, params);
            return res.json({
                code: 0,
                msg: "success",
                data: result,
            });
        } catch (error) {
            console.error(error.message);
            return res.status(500).json({
                code: 1,
                msg: "服务器内部错误，修改库存信息失败。",
            });
        }
    }
    /** 添加库存日志 */
    async addInventoryLog(req, res) {
        try {
            const params = req.body;
            const result = await this.inventoryModel.addInventoryLog(params);
            return res.json({
                code: 0,
                msg: "success",
                data: result,
            });
        } catch (error) {
            console.error(error.message);
            return res.status(500).json({
                code: 1,
                msg: "服务器内部错误，添加库存日志失败。",
            });
        }
    }
    /** 添加库存 */
    async addInventory(req, res) {
        try {
            const params = req.body;
            console.log('前端传递的原始参数:', params);
            console.log('参数类型检查:', {
                goods_id: typeof params.goods_id,
                house_id: typeof params.house_id,
                current_stock: typeof params.current_stock,
                safety_stock: typeof params.safety_stock,
                notes: typeof params.notes
            });
            
            const result = await this.inventoryModel.addInventory(params);
            return res.json({
                code: 0,
                msg: "success",
                data: result,
            });
        } catch (error) {
            console.error('添加库存错误:', error.message);
            return res.status(500).json({
                code: 1,
                msg: error.message || "服务器内部错误，添加库存失败。",
            });
        }
    }
    /** 获取库存日志 */
    async getInventoryLog(req, res) {
        try {
            const  stock_id  = req.query.stock_id;
            const result = await this.inventoryModel.getInventoryLog(stock_id);
            return res.json({
                code: 0,
                msg: "success",
                data: result,
            });
        } catch (error) {
            console.error(error.message);
            return res.status(500).json({
                code: 1,
                msg: "服务器内部错误，获取库存日志失败。",
            });
        }
    }
    /** 获取仓库区域信息 */
    async getAreaInfo(req, res) {
        try {
            const  house_id  = req.query.house_id;
            const result = await this.inventoryModel.getAreaInfo(house_id);
            return res.json({
                code: 0,
                msg: "success",
                data: result,
               
            });
        } catch (error) {
            console.error(error.message);
            return res.status(500).json({
                code: 1,
                msg: "服务器内部错误，获取仓库区域信息失败。",
            });
        }
    }
    /** 获取仓库信息 */
    async getHouseInfoById(req, res) {
        try {
            const  house_id  = req.query.house_id;
            const result = await this.inventoryModel.getHouseInfoById(house_id);
            return res.json({
                code: 0,
                msg: "success",
                data: result,
            });
        } catch (error) {
            console.error(error.message);
            return res.status(500).json({
                code: 1,
                msg: "服务器内部错误，获取仓库信息失败。",
            });
        }
    }
    /** 根据商品id获取型号规格当前数量 */
    async getInventoryByGoodsId(req, res) {
        try {
            const  goods_id  = req.query.goods_id;
            const result = await this.inventoryModel.getInventoryByGoodsId(goods_id);
            return res.json({
                code: 0,
                msg: "success",
                data: result,
            });
        } catch (error) {
            console.error(error.message);
            return res.status(500).json({
                code: 1,
                msg: "服务器内部错误，根据商品id获取型号规格当前数量失败。",
            });
        }
    }
}

module.exports = InventoryController;